package org.example.component.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.component.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MailServiceImpl implements MailService {

    @Autowired
    private MailSender mailSender;

    @Value("${spring.mail.from}")
    private String from;

    /**
     * 发送邮件
     * @param to 发送给谁
     * @param subject 主题
     * @param content 内容
     */
    @Override
    public void sendMail(String to, String subject, String content) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        // 谁发的
        mailMessage.setFrom(from);
        // 发给谁
        mailMessage.setTo(to);
        // 邮件主题
        mailMessage.setSubject(subject);
        // 邮件内容
        mailMessage.setText(content);
        // 发送
        mailSender.send(mailMessage);
        log.info("发送邮件成功: {}", mailMessage.toString());
    }
}
